﻿Imports HMSLibrary
Imports HMSDataLayer

Public Class PurchaseWindow
    Implements IHMSData

    Dim pType As PurchaseEnum

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Sub New(ByVal FormType As FormsList)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        If FormType = FormsList.Purchase Then
            SubtotalTextBlock.Visibility = Visibility.Visible
            SubtotalTextBox.Visibility = Visibility.Visible
            OtherChargesTextBlock.Visibility = Visibility.Visible
            OtherchargesTextBox.Visibility = Visibility.Visible
            ModuleNameRun.Text = "Purchase Invoice"
            pType = PurchaseEnum.Invoice
        Else
            ModuleNameRun.Text = "Purchase Order"
            NoTextBlock.Text = "PO No.:"
            InvoiceNoTextBox.IsReadOnly = True
            pType = PurchaseEnum.Order
        End If
    End Sub

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CancelButton.Click
        RemarksStackPanel.Visibility = Visibility.Visible
        TaxDiscountDataGrid.Visibility = Visibility.Collapsed
        TaxDiscountStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub PurchaseWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        NewPIPO()
    End Sub

    Private Sub PurchaseWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        DragMove()
    End Sub

    Private Sub TransSidePanel_AddButtonClickEvent() Handles TransSidePanel.AddButtonClickEvent
        AddNew()
        NewPIPO()
    End Sub

    Private Sub ApplyPermission()
        If AppCommon.loginInfo.IsSuperUser = True Then
            TransSidePanel.AddVisibility = Visibility.Visible
            TransSidePanel.FindVisibility = Visibility.Visible
            TransSidePanel.SaveVisibility = Visibility.Visible
            TransSidePanel.PrintVisibility = Visibility.Visible
            Exit Sub
        End If
        Dim mp As ModulePermissions
        mp = (From ml In AppCommon.loginInfo.ModulesList Where ml.Name = HMSFormName.Purchase Select ml).FirstOrDefault
        If mp.Add = False Then TransSidePanel.AddVisibility = Visibility.Collapsed Else TransSidePanel.AddVisibility = Visibility.Visible
        If mp.Edit = False Then TransSidePanel.FindVisibility = Visibility.Collapsed Else TransSidePanel.FindVisibility = Visibility.Visible
        If mp.Print = False Then TransSidePanel.PrintVisibility = Visibility.Collapsed Else TransSidePanel.PrintVisibility = Visibility.Visible
    End Sub

    Private Sub TransSidePanel_BackButtonClickEvent() Handles TransSidePanel.BackButtonClickEvent
        ApplyPermission()
        TransSidePanel.BackVisibility = Visibility.Collapsed
        BackView()
    End Sub

    Private Sub TransSidePanel_CancelButtonClickEvent() Handles TransSidePanel.CancelButtonClickEvent
        Me.Close()
    End Sub

    Private Sub TransSidePanel_FindButtonClickEvent() Handles TransSidePanel.FindButtonClickEvent
        TransSidePanel.PrintVisibility = Visibility.Collapsed
        TransSidePanel.AddVisibility = Visibility.Collapsed
        TransSidePanel.SaveVisibility = Visibility.Collapsed
        TransSidePanel.FindVisibility = Visibility.Collapsed
        TransSidePanel.BackVisibility = Visibility.Visible
        FindData()
    End Sub

    Private Sub OtherChargesTextBlock_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles OtherChargesTextBlock.MouseLeftButtonDown
        RemarksStackPanel.Visibility = Visibility.Collapsed
        TaxDiscountDataGrid.Visibility = Visibility.Visible
        TaxDiscountStackPanel.Visibility = Visibility.Visible
    End Sub

    Dim PIPOObject As Purchase

    Private Sub NewPIPO()
        ActionMode = FormAction.ADD
        BillDatePicker.SelectedDate = Date.Now
        Dim dataProvider As New MasterDataLayer()

        Dim lstCl As List(Of Vendor)
        lstCl = dataProvider.GetVendorList()
        VendorComboBox.ItemsSource = lstCl

        PIPOObject = New Purchase
        Me.DataContext = PIPOObject

        BillItemsDataGrid.ItemsSource = PIPOObject.PIPOItems
        TaxDiscountDataGrid.ItemsSource = PIPOObject.PIPODisTax
    End Sub

    Private Sub TransSidePanel_SaveButtonClickEvent() Handles TransSidePanel.SaveButtonClickEvent
        If DataValidation() = False Then Exit Sub
        SaveData()
    End Sub

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        InvoiceNoTextBox.Text = String.Empty
        VendorComboBox.SelectedIndex = -1
        If SubtotalTextBox.Visibility = Visibility.Visible Then SubtotalTextBox.Text = String.Empty
        If OtherchargesTextBox.Visibility = Visibility.Visible Then OtherchargesTextBox.Text = String.Empty
        NetamountTextBox.Text = String.Empty
        NewPIPO()
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainStack.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        If String.IsNullOrEmpty(InvoiceNoTextBox.Text.Trim) And pType = PurchaseEnum.Invoice Then
            CommonMessages.ShowValidationMsg("Please enter invoice no!")
            Return False
        End If

        If String.IsNullOrEmpty(VendorComboBox.Text.Trim) Then
            CommonMessages.ShowValidationMsg("Please select vendor!")
            Return False
        End If
        If BillDatePicker.SelectedDate Is Nothing Then
            CommonMessages.ShowValidationMsg("Please select date!")
            Return False
        End If
        Return True
    End Function

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainStack.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As New PurchaseDataLayer
        Dim DataCollection As Object
        DataCollection = DataObject.GetPurchaseInvoice
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData

    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        PIPOObject.NetAmt = Val(NetamountTextBox.Text)
        PIPOObject.OtherCharges = Val(OtherchargesTextBox.Text)
        PIPOObject.SubTotal = Val(SubtotalTextBox.Text)
        PIPOObject.PIPONo = InvoiceNoTextBox.Text
        PIPOObject.VendorID = VendorComboBox.SelectedValue
        PIPOObject.CreatedBy = AppCommon.loginInfo.UserID
        Dim dataProvider As New PurchaseDataLayer()
        If dataProvider.SavePOPI(PIPOObject, ActionMode, pType) = 0 Then Return False
        If pType = PurchaseEnum.Order Then InvoiceNoTextBox.Text = PIPOObject.PIPOID
        Return True
    End Function

    Private Sub ItemNameTextBox_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
        If e.Key = Key.F1 Then
            Dim lup As New LookupsWindow(LookupsList.InventoryItemsList)
            lup.Owner = Me
            lup.ShowDialog()
            Dim ItemObject As ItemsService
            ItemObject = lup.SelectedObject
            If lup.SelectedObject Is Nothing Then Exit Sub
            Dim billItemObject As BillItems
            billItemObject = DirectCast(sender, System.Windows.Controls.TextBox).DataContext
            If ItemObject.Name Is Nothing Then Exit Sub
            billItemObject.ItemName = ItemObject.Name
            billItemObject.ItemID = ItemObject.ID
            billItemObject.Rate = ItemObject.Rate
            billItemObject.UOMID = ItemObject.UOM
            billItemObject.UOMName = ItemObject.UOMName
        Else
            sender.text = ""
        End If
    End Sub

    Private Sub UOMNameTextBox_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
        If e.Key = Key.F1 Then
            Dim lup As New LookupsWindow(LookupsList.UOMList)
            lup.Owner = Me
            lup.ShowDialog()
            If lup.SelectedObject Is Nothing Then Exit Sub
            Dim ItemObject As Lookups
            ItemObject = lup.SelectedObject
            sender.text = ItemObject.Name
            sender.tag = ItemObject.ID
        End If
    End Sub

    Private Sub TaxDiscountTextBox_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
        If e.Key = Key.F1 Then
            Dim lup As New LookupsWindow(LookupsList.BillCharges)
            lup.Owner = Me
            lup.ShowDialog()
            If lup.SelectedObject Is Nothing Then Exit Sub
            Dim ItemObject As Lookups
            ItemObject = lup.SelectedObject
            Dim billDisTaxtObject As BillDiscountTax
            billDisTaxtObject = DirectCast(sender, System.Windows.Controls.TextBox).DataContext
            If ItemObject.Name Is Nothing Then Exit Sub
            billDisTaxtObject.TaxDiscName = ItemObject.Name
            billDisTaxtObject.TaxDiscID = ItemObject.ID
            billDisTaxtObject.Rate = ItemObject.Rate
            billDisTaxtObject.Amount = (Val(SubtotalTextBox.Text) * ItemObject.Rate) / 100
            ItemsOtherTotal()
        End If
    End Sub

    Private Sub ItemsOtherTotal()
        Dim subTotal As Double = 0
        For Each sbItem In PIPOObject.PIPODisTax
            sbItem.Amount = (Val(SubtotalTextBox.Text) * sbItem.Rate) / 100
            subTotal = subTotal + sbItem.Amount
        Next
        OtherChargesTextBox.Text = subTotal
        CalculateNetAmt()
    End Sub

    Private Sub CalculateNetAmt()
        NetAmountTextBox.Text = Val(SubTotalTextBox.Text) + Val(OtherChargesTextBox.Text)
    End Sub

    Private Sub BillItemsDataGrid_CellEditEnding(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs) Handles BillItemsDataGrid.CellEditEnding
        Dim billItemObject As BillItems = e.Row.DataContext
        If billItemObject.Rate = 0 Or billItemObject.Qty = 0 Then Exit Sub
        billItemObject.Amount = billItemObject.Rate * billItemObject.Qty
        ItemsSubTotal()
    End Sub

    Private Sub ItemsSubTotal()
        Dim subTotal As Double = 0
        For Each sbItem In PIPOObject.PIPOItems
            subTotal = subTotal + sbItem.Amount
        Next
        SubTotalTextBox.Text = subTotal
        ItemsOtherTotal()
    End Sub

    Private Sub TaxDiscountDataGrid_CellEditEnding(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs) Handles TaxDiscountDataGrid.CellEditEnding
        Dim billTaxObject As BillDiscountTax = e.Row.DataContext
        If billTaxObject.Rate = 0 Or Val(SubTotalTextBox.Text) = 0 Then Exit Sub
        billTaxObject.Amount = (Val(SubTotalTextBox.Text) * billTaxObject.Rate) / 100
        ItemsOtherTotal()
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
